package de.artmann.powerfit;

import android.app.Activity;
import android.os.AsyncTask;
import android.webkit.WebView;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * Created by David on 01.09.2014.
 */
public class NetworkActivity extends Activity {
    public static final String WIFI = "Wi-Fi",
                                ANY = "Any",
                                URL = "http://scoberoelsbach.de/powerfit/oeffnungszeiten/";
    private static boolean wifiConnected = false,
                            mobileConnected = false,
                            refreshDisplay = false;
    private static String sPref = null;

    public void loadPage() {
        if((sPref.equals(ANY)) && (wifiConnected || mobileConnected)) {
            new DownloadXmlTask().execute(URL);
        }
        else if((sPref.equals(WIFI)) && (wifiConnected)) {
            new DownloadXmlTask().execute(URL);
        }
        else {
            //Fehler anzeigen lassen
        }
    }

    private class DownloadXmlTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            try {
                return loadXmlFromNetwork(urls[0]);
            } catch (IOException e) {
                return getResources().getString(R.string.connection_error);
            } catch (XmlPullParserException e) {
                return getResources().getString(R.string.xml_error);
            }
        }

        @Override
        protected void onPostExecute(String s) {
            setContentView(R.layout.activity_activity1);
            //ListView listView = (ListView) findViewById(R.id.listView);
            //listView.setAdapter(new SimpleAdapter());
            WebView myWebView = (WebView) findViewById(R.id.webview);
            myWebView.loadData(s, "text/html", null);
        }
    }

    private String loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException
    {
        InputStream in = null;
        PowerFitXmlParser pfXmlParser = new PowerFitXmlParser();
        List<Entry> entries = null;
        String info = null;
        Calendar now = Calendar.getInstance();
        DateFormat formatter = new SimpleDateFormat("MMM dd h:mmaa");

        //SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        //boolean pref = sp.getBoolean("summaryPref", false);

        StringBuilder htmlString = new StringBuilder();
        htmlString.append("<h3>" + getResources().getString(R.string.header_string) + "</h3>");
        htmlString.append("<em>" + getResources().getString(R.string.updated) + " " +
                formatter.format(now.getTime()) + "</em>");

        try {
            in = downloadUrl(urlString);
            entries = pfXmlParser.parse(in);
        } finally {
            if(in != null) {
                in.close();
            }
        }

        for(Entry entry : entries) {
            htmlString.append("<p>");
            htmlString.append(entry._info);
            htmlString.append("</p>");
            //if(pref) {
            //    htmlString.append(entry.summary)
            //}
        }
        return htmlString.toString();
    }

    private InputStream downloadUrl(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();
        return conn.getInputStream();
    }
}


